package com.bosssoft.setp.model.business.course;

import com.bosssoft.setp.handler.CourseDataProxy;
import com.bosssoft.setp.handler.TagsFetchHandlerImpl;
import xyz.erupt.annotation.Erupt;
import xyz.erupt.annotation.EruptField;
import xyz.erupt.annotation.PreDataProxy;
import xyz.erupt.annotation.sub_erupt.Power;
import xyz.erupt.annotation.sub_field.*;
import xyz.erupt.annotation.sub_field.sub_edit.*;
import xyz.erupt.jpa.model.BaseModel;
import javax.persistence.*;
import java.util.Date;
import java.util.Set;

/**
 * 课程实体的类定义
 *
 * @author clt
 */
@Entity
@Table(name = "setp_course")
@Erupt(name = "课程管理",
        power = @Power(add = true, delete = true,
                edit = true, query = true,
                importable = true, export = true)
)
@PreDataProxy(CourseDataProxy.class)
public class Course extends BaseModel {
    @ManyToOne
    @EruptField(
            edit = @Edit(
                    title = "课程类别",
                    type = EditType.REFERENCE_TREE,
                    search = @Search(vague = true),
                    notNull = true,
                    referenceTreeType = @ReferenceTreeType(
                            id = "id", label = "name",
                            pid = "parent.id"
                    )
            )
    )
    private CourseType courseType;

    @EruptField(
            views = @View(title = "课程名", sortable = true),
            edit = @Edit(title = "课程名", inputType = @InputType, notNull = true, search = @Search(vague = true))
    )
    private String name;

    @EruptField(
            views = @View(title = "编码", sortable = true),
            edit = @Edit(title = "编码", inputType = @InputType,readonly = @Readonly(edit = false), search = @Search(vague = true),placeHolder = "新增不需要输入")
    )
    private String code;

    @EruptField(
            views = @View(title = "基本信息", sortable = true),
            edit = @Edit(title = "基本信息", type = EditType.TEXTAREA, notNull = true)
    )
    private String basicInfo;

    @EruptField(
            views = @View(title = "课程时长", sortable = true),
            edit = @Edit(title = "课程时长", numberType = @NumberType(min = 0), notNull = true)
    )
    private Integer courseDuration;

    @EruptField(
            views = @View(title = "封面", sortable = true),
            edit = @Edit(title = "封面", type = EditType.ATTACHMENT, notNull = true,
                    attachmentType = @AttachmentType(type = AttachmentType.Type.IMAGE))
    )
    private String coverPaperUrl;

    @EruptField(
            views = @View(title = "支持设备类型", sortable = true),
            edit = @Edit(title = "支持设备类型",
                    type = EditType.TAGS,
                    notNull = true,
                    tagsType = @TagsType(
                            fetchHandlerParams = {"设备类别"},
                            fetchHandler = TagsFetchHandlerImpl.class
                    ))
    )
    private String suportDeviceType;

    @Lob
    @EruptField(
            views = @View(title = "课程话术", type = ViewType.HTML),
            edit = @Edit(title = "课程话术",
                    type = EditType.HTML_EDITOR,
                    htmlEditorType = @HtmlEditorType(HtmlEditorType.Type.UEDITOR))
    )
    private String verbalTrickTemplate;

    @EruptField(
            views = @View(title = "课程话术模板"),
            edit = @Edit(title = "课程话术模板",
                    type = EditType.ATTACHMENT,
                    attachmentType = @AttachmentType)
    )
    private String verbalTrickTemplateAttachment;

    @Lob
    @EruptField(
            views = @View(title = "课后作业", type = ViewType.HTML),
            edit = @Edit(title = "课后作业",
                    type = EditType.HTML_EDITOR,
                    htmlEditorType = @HtmlEditorType(HtmlEditorType.Type.UEDITOR))
    )
    private String busyworkTemplate;

    @EruptField(
            views = @View(title = "课后作业模板"),
            edit = @Edit(title = "课后作业模板",
                    type = EditType.ATTACHMENT,
                    attachmentType = @AttachmentType)
    )
    private String busyworkTemplateAttachment;


    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name = "course_id")
    @EruptField(
            edit = @Edit(title = "课程详细", type = EditType.TAB_TABLE_ADD)
    )
    private Set<CourseDetail> courseDetails;

    @EruptField(
            views = @View(title = "创建人"),
            edit = @Edit(title = "更新时间",readonly  = @Readonly)
    )
    private  String createUser;

    @EruptField(
            views = @View(title = "创建时间", sortable = true),
            edit = @Edit(title = "创建时间",readonly  = @Readonly)
    )
    private Date createTime;

    @EruptField(
            views = @View(title = "更新人"),
            edit = @Edit(title = "更新人",readonly  = @Readonly)
    )
    private  String updateUser;

    @EruptField(
            views = @View(title = "更新时间", sortable = true),
            edit = @Edit(title = "更新时间",readonly  = @Readonly)
    )
    private  Date updateTime;

    public void setCreateUser(String name) {
        this.createUser = name;
    }

    public void setUpdateUser(String name) {
        this.updateUser = name;
    }

    public void setCreateTime(Date date) {
        this.createTime = date;
    }

    public void setUpdateTime(Date date) { this.updateTime = date; }

    public Object getCourseTypeId() {
        return courseType.getId();
    }

    public String getName() {
        return name;
    }

    public String getBasicInfo() {
        return basicInfo;
    }

    public boolean isVerbalTrickTemplateNull() {
        return verbalTrickTemplate == null;
    }

    public String getVerbalTrickTemplate() {
        return verbalTrickTemplate;
    }

    public String getVerbalTrickTemplateAttachment() {
        return verbalTrickTemplateAttachment;
    }

    public boolean isBusyworkTemplateNull() {
        return busyworkTemplate == null;
    }

    public String getBusyworkTemplate() {
        return busyworkTemplate;
    }

    public String getBusyworkTemplateAttachment() {
        return busyworkTemplateAttachment;
    }

    public Set<CourseDetail> getCourseDetails() {
        return courseDetails;
    }

    public String getCode() {
        return this.code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    /**
     * 是否有课后作业
     * @return
     */
    public boolean isBusyworkUseful() {
        Boolean result = null;
        if(this.busyworkTemplate == null && this.busyworkTemplateAttachment == null){
            result =  false;
        }else {
            result =   true;
        }
        return result;
    }

    /**
     * 是否有课程话术
     * @return
     */
    public boolean isVerbalTrickUseful() {
        Boolean result = null;
        if(this.verbalTrickTemplate == null && this.verbalTrickTemplateAttachment == null){
            result =  false;
        }else {
            result =   true;
        }
        return result;
    }
}